// import api from 'API';
// import myStream from 'API/Stream.js';
import api from '../../stream/index';
import myStream from '../../stream/Stream';
import mockData from '../../mock_data';

const modal = weex.requireModule('modal');
export default {
  state: {
    tabsData: []
  },
  mutations: {
    setTabsData(state, data) {
      state.tabsData = data;
    },
    updateTabsData(state, {
      index,
      data
    }) {
      state.tabsData[index].tab_data = data;
    }
  },
  actions: {
    getTabsData({
      state,
      commit,
      rootState
    }, option) {
      return new Promise((resolve, reject) => {
        api.mainInfo.post(option).then((data) => {
          commit('setTabsData', data.data.tabs);
          resolve(data.data.tabs);
        }, (error) => {
          console.log(error);
          reject(data.data);
        });
      });
    },
    getTabPageData({
      state,
      commit,
      rootState
    }, option) {
      return new Promise((resolve, reject) => {
        new myStream(option.url).post(option.params).then((result) => {
          let data = result.data.sections;
          if (!data.length) {
            data = mockData.tabPage[option.params.page_id].data;
          }
          commit('updateTabsData', {
            index: option.params.tab_index,
            data: data
          });
          modal.toast({
            message: data.sections.length,
            duration: 2
          });
          resolve(data.data);
        }, (error) => {
          console.log(error);
          reject(data.data);
        });
      });
    }
  }
};